<HEAD>
  <SCRIPT SRC="../ganja.js"></SCRIPT>
</HEAD>
<BODY><SCRIPT>
// Create a Clifford Algebra with 2,0,1 metric.
Algebra(2,0,1,()=>{

  // A simple illustrative example that calculates the position of a 2D
  // object from its given projection on two known 1D cameras.

  // the PGA identities we need.
  var point = (x,y)=>!(1e0 + x*1e1 + y*1e2),
      line  = (a,b,c)=>a*1e1 + b*1e2 + c*1e0;
  
  // Define some points and lines.
  var A=point(0,0.5), B=point(-0.5,1), C=point(0.5,1);
  
  // Two cameras and camera line.
  var CA=point(-0.5,-1.5), CB=point(0.5,-1.5), cl=line(0,1,1);
  
  // Images of A,B,C on camera line. 
  var iaA = cl^(CA&A), iaB = cl^(CA&B), iaC = cl^(CA&C);
  var ibA = cl^(CB&A), ibB = cl^(CB&B), ibC = cl^(CB&C);
  
  // At this point the problem is constructed. We now do the inverse
  // and reconstruct the 3D positions of the points from their projections.
  
  // Lines from camera through pojections.
  var CAA = CA & iaA, CAB = CA & iaB, CAC = CA & iaC;
  var CBA = CB & ibA, CBB = CB & ibB, CBC = CB & ibC;
  
  // Graph it
  document.body.appendChild(this.graph(()=>{
      var s = performance.now()*0.001%5, res=[0x224488,['Start from projected points','Construct rays camera A','Construct rays camera B','Intersect corresponding rays','Find object'][Math.floor(s)]];
      if (s>=0) res = res.concat([cl,CA,"Cam A",CB,"Cam B",iaA, iaB, iaC, ibA, ibB, ibC]); 
      if (s>=1) res = res.concat([0x882288,CAA,CAB,CAC]); 
      if (s>=2) res = res.concat([0x00AA88,CBA,CBB,CBC]); 
      if (s>=3) res = res.concat([0x224488,CBA^CAA,CBB^CAB,CBC^CAC]); 
      if (s>=4) res = res.concat([0x008844,[CBA^CAA,CBB^CAB],[CBA^CAA,CBC^CAC]])
      return res;
  },{grid:true, labels:true, lineWidth:2, animate:true}));
});
</SCRIPT></BODY>